// 登录与注册模块
import { reqGetCode, reqUserRegister, reqUserLogin, reqUserInfo, reqLogout } from "@/api"
import { setToken, getToken, removeToken } from "@/utils/token";
const state = {
    code: "",
    token: getToken(),
    userInfo: {}
};
const mutations = {
    GETCODE(state, code) {
        state.code = code;
    },
    USERLOGIN(state, token) {
        state.token = token;
    },
    GETUSERINFO(state, userInfo) {
        state.userInfo = userInfo
    },
    // 清除本地数据
    CLEAR(state) {
        state.token = "";
        state.userInfo = {};
        removeToken();
    }
};
const actions = {
    // 获取验证码
    async getCode({ commit, state, dispatch }, phone) {
        let result = await reqGetCode(phone);
        if (result.code == 200) {
            commit("GETCODE", result.data)
            return "ok"
        } else {
            console.log(result);
            return Promise.reject(new Error('faile'))
        }
    },
    // 用户注册
    async userRegister({ commit }, user) {
        let result = await reqUserRegister(user);
        if (result.code == 200) {
            // console.log(result);
            return 'ok';
        } else {
            console.log(result);
            return Promise.reject(new Error('faile'));
        }
    },
    //用户登录
    async userLogin({ commit }, user) {
        let result = await reqUserLogin(user);
        // 服务器下发的token，是唯一标识
        if (result.code == 200) {
            commit("USERLOGIN", result.data.token);
            // localStorage.setItem("TOKEN", result.data.token);
            setToken(result.data.token);
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'));
        }
    },
    // 获取用户信息
    async getUserInfo({ commit }) {
        let result = await reqUserInfo();
        if (result.code == 200) {
            // 提交用户信息
            console.log(result);
            commit("GETUSERINFO", result.data);
            return 'ok';
        }
    },
    // 退出登录
    async userLogout({ commit }) {
        // 只是想服务器发起一次请求，通知服务器清除token
        let result = await reqLogout();
        // action里面
        if (result.code == 200) {
            commit("CLEAR");
            return 'ok'
        } else {
            return Promise.reject(new Error('faile'));
        }
    }
};
const getters = {};
export default {
    state,
    mutations,
    actions,
    getters
}
